<template>
  <Layer :layer="layer" @confirm="submit" ref="layerDom">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right: 30px">
      <el-form-item label="分类名称" prop="softwareTypeName">
        <el-input v-model="form.softwareTypeName" placeholder="请输入分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" prop="softwareTypeDescription">
        <el-input v-model="form.softwareTypeDescription" placeholder="亲输入分类描述">
        </el-input>
      </el-form-item>
      <el-form-item label="分类状态" prop="softwareTypeStatus">
        <el-radio-group v-model="form.softwareTypeStatus" size="small" style="width: 178px">
          <el-radio-button label="false">关闭</el-radio-button>
          <el-radio-button label="true">开启</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上级类目" prop="parentId">
        <el-tree-select style="width:100%" v-model="form.parentId" :data="menuOptions" check-strictly/>
      </el-form-item>
    </el-form>
  </Layer>
</template>

<script lang="ts" setup>
import type {LayerType} from "@/components/layer/index.vue";
import Layer from "@/components/layer/index.vue";
import {Ref, ref} from "vue";
import type {ElFormItemContext} from "element-plus/lib/el-form/src/token";
import {add, getData, update} from "@/api/article/software";
import {ElMessage} from "element-plus";

const props = defineProps({
  layer: {
    type: Object,
    default() {
      return {
        show: false,
        title: "",
        showButton: true,
      };
    },
  },
});
const ruleForm: Ref<ElFormItemContext | null> = ref(null);
const menuOptions = ref([
  {
    value: '0',
    label: '顶级菜单',
  }
])
const layerDom: Ref<LayerType | null> = ref(null);
const emits = defineEmits(["getTableData"]);
let form = ref({
  softwareTypeName: "",
  softwareTypeDescription: "",
  softwareTypeStatus: true,
  parentId: "0",
});
const rules = {
  softwareTypeName: [{required: true, message: "请输入分类名称", trigger: "blur"}],
};
init();

function init() {
  // 用于判断新增还是编辑功能
  if (props.layer.row) {
    form.value = JSON.parse(JSON.stringify(props.layer.row)); // 数量量少的直接使用这个转
    //获取当前的字典
  } else {
  }
  getData({}).then(({data}) => {
    // 格式转换
    data.forEach((item: any) => {
      item.value = item.softwareTypeId;
      item.label = item.softwareTypeName;
      // children 为空不转换
      if (!item.children) return;
      item.children.forEach((item: any) => {
        item.value = item.softwareTypeId;
        item.label = item.softwareTypeName;
      });
    });
    menuOptions.value = menuOptions.value.concat(data)
  })
}

const submit = () => {
  ruleForm.value.validate((valid: boolean) => {
    if (valid) {
      let params = form.value;
      if (props.layer.row) {
        updateForm(params);
      } else {
        addForm(params);
      }
    } else {
      return false;
    }
  });
};

// 新增提交事件
const addForm = (params: any) => {
  params.parentSoftwareTypeId = params.parentId;
  add(params).then((res) => {
    ElMessage({
      type: "success",
      message: "新增成功",
    });
    props.layer.show = false;
    emits("getTableData", true);
  });
};

// 编辑提交事件
const updateForm = (params: any) => {
  params.parentSoftwareTypeId = params.parentId;
  update(params).then((res) => {
    ElMessage({
      type: "success",
      message: "编辑成功",
    });
    emits("getTableData", false);
  });
};
</script>

<style lang="scss" scoped></style>